<script>
	import Tabber from "@/common/homeCnter.vue"
	import toMaintain from "serve/api/toMaintain.js"

	import login from "@/serve/api/login.js";
	import {
		mapMutations
	} from "vuex"
	export default {
		onLaunch: async function() {
				let that = this;
			if (uni.getStorageSync('token')) {
				const res = await toMaintain.selectSysUserById({})
				if (res) {
					this.setUserinfo(res.data.data);
				}
				// #ifdef APP-PLUS
				const _handlePush = function(message) {
					console.log(message)
					if(message.payload){
						let url = JSON.parse(message.payload).url;
						uni.navigateTo({
							url: url
						});
					}
				};
				// plus.push.addEventListener('click', _handlePush);
				plus.push.addEventListener('receive', _handlePush);
				
				// #endif
				
				uni.switchTab({
					url: "/pages/tabbar/home/home"
				})
			} else {
				this.reLaunch("/pages/register/login/login")
			}

			
	
			
			// #ifdef APP-PLUS
			plus.runtime.getProperty(plus.runtime.appid, async function(widgetInfo) {
				let parme = {
					version: widgetInfo.version,
				}
				const info = await login.UpdateInfo(parme);
				
				if (info.data.data.update) {
					if (info.data.data.wgtUpdate) {
						that.downWgt(info.data.data.wgtUrl)
					} else {
						uni.showModal({
							title: '更新提示',
							content:'是否选择更新',
							success: showResult => {
								if (showResult.confirm) {
									that.updateAppAll(info.data.data[plus.os.name])
								}
							},
						})
					}
				}
			});
			// #endif

		},
		methods: {
			...mapMutations(['setUserinfo']),

			/** 整包更新 */
			updateAppAll(openUrl) {
				if (plus.os.name === 'iOS') {
					plus.runtime.openURL(openUrl)
				} else {
					
					this.tip("正在后台下载")
					var dtask = plus.downloader.createDownload(openUrl, {}, function(d, status) {
						if (status === 200) { 
							var path = d.filename
							plus.runtime.install(path)
						} else {
							alert('下载失败' + status)
						}
					})
					dtask.start()
				}
			},
			/** App资源在线升级更新 */ 
			// 下载wgt文件
			downWgt(wgtUrl) {
				let that = this;
				plus.nativeUI.showWaiting('下载更新文件...')
				plus.downloader.createDownload(wgtUrl, {
					filename: '_doc/update/'
				}, function(d, status) {
					if (status == 200) {
						that.installWgt(d.filename) 
					} else { 
						plus.nativeUI.alert('下载更新文件失败！')
					}
					plus.nativeUI.closeWaiting()
				}).start()
			},
			// 更新应用资源包（wgt文件）
			installWgt(path) { 
					// console.log(path)
				plus.nativeUI.showWaiting('安装更新文件...')
				plus.runtime.install(path, {}, function() {
					plus.nativeUI.closeWaiting()
					plus.runtime.restart()
					// console.log('安装更新文件成功！')
					// plus.nativeUI.alert('应用资源更新完成！', function() {
					// 	plus.runtime.restart()
					// })
				}, function(e) {
					plus.nativeUI.closeWaiting()
					plus.nativeUI.alert('安装更新文件失败[' + e.code + ']：' + e.message)
				})
			}
		},
		onShow: function() {
			console.log('App Show')

		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	@import "./static/css/init.css";
	/*每个页面公共css */
	/* #ifndef APP-PLUS-NVUE */
	* {
		margin: 0;
		padding: 0;
	}

	page {
		width: 750rpx;
		background: #EFF2F5;
		/* margin-bottom: 88rpx; */
		/* overflow: hidden; */
	}

	view {
		font-size: 28rpx;
		line-height: 1.8;

	}

	.uni-card {
		border: none;
		margin: 0 auto;
	}

	/* 头部外层的高度结构 */
	uni-page-head .uni-page-head {
		height: 88upx;
		padding-right: 44upx;
		display: flex;
		align-items: center;
		border-bottom: 1px solid rgba(0, 0, 0, 0.06);
	}

	/* 头部标题的样式 */
	uni-page-head .uni-page-head__title {
		font-family: 'PingFang_Regular' !important;
		color: #333333 !important;
		font-size: 32upx !important;
	}

	uni-page-head .uni-page-head-ft {
		margin-top: 10upx;
	}

	/* 头部标题层的高度，不设置的话页面的内容会向上被头部遮挡 */
	uni-page-head .uni-page-head~.uni-placeholder {
		height: 88upx;
	}

	uni-page-head {
		padding-right: 23px;
	}

	/* 头部高度改变了原先的页面高度兼容也要想对应的改变 */
	uni-page-head[uni-page-head-type=default]~uni-page-wrapper {
		height: calc(100% - 88upx);
	}

	/* #endif */
</style>
